package com.che.software.testato.web.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.che.software.testato.business.ActivityManager;
import com.che.software.testato.business.ElementManager;
import com.che.software.testato.business.IterationManager;
import com.che.software.testato.business.ProceduralDiagramManager;
import com.che.software.testato.business.ProjectManager;
import com.che.software.testato.business.TestCaseManager;
import com.che.software.testato.business.TransitionManager;
import com.che.software.testato.business.VariantManager;
import com.che.software.testato.business.VersionManager;
import com.che.software.testato.business.exception.ActivitySearchManagerException;
import com.che.software.testato.business.exception.ElementSearchManagerException;
import com.che.software.testato.business.exception.TestCaseSearchManagerException;
import com.che.software.testato.business.exception.TransitionSearchManagerException;
import com.che.software.testato.business.exception.VariantCreationManagerException;
import com.che.software.testato.domain.entity.Activity;
import com.che.software.testato.domain.entity.Element;
import com.che.software.testato.domain.entity.Iteration;
import com.che.software.testato.domain.entity.TestCase;
import com.che.software.testato.domain.entity.Transition;
import com.che.software.testato.domain.entity.search.ElementSearch;
import com.che.software.testato.domain.entity.search.IterationSearch;
import com.che.software.testato.domain.entity.search.ProjectSearch;
import com.che.software.testato.domain.entity.search.TransitionSearch;
import com.che.software.testato.domain.entity.search.VersionSearch;
import com.che.software.testato.domain.enumeration.AssignmentStatus;
import com.che.software.testato.util.jsf.faces.FacesUtil;
import com.che.software.testato.web.controller.acontroller.AbstractController;

/**
 * Controller used to manage the home page and its components.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @see AbstractController, Serializable.
 * @since July, 2011.
 * 
 * This file is part of Testato.
 * 
 * Testato is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * Testato is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Testato's logo is a creation of Arrioch (http://arrioch.deviantart.com/) and it's distributed under the terms of the Creative Commons License.
 */
@Component("homeController")
@ManagedBean(name = "homeController")
@Scope("session")
@SessionScoped
public class HomeController extends AbstractController implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(HomeController.class);
	private static final long serialVersionUID = -6297516803158687609L;

	/**
	 * Members.
	 */
	@Autowired
	private transient ActivityManager activityManager;
	@Autowired
	private transient ElementManager elementManager;
	private int currentTestCaseId;
	private List<Activity> activitiesToDescribe;
	private List<Element> elements;
	private List<Iteration> prioritizationsToDo;
	private List<Transition> transitions;
	private List<TestCase> testCasesVariantsToGenerate;
	@Autowired
	private transient IterationManager iterationManager;
	@Autowired
	private transient ProceduralDiagramManager proceduralDiagramManager;
	@Autowired
	private transient ProjectManager projectManager;
	@Autowired
	private transient TestCaseManager testCaseManager;
	@Autowired
	private transient TransitionManager transitionManager;
	@Autowired
	private transient VariantManager variantManager;
	@Autowired
	private transient VersionManager versionManager;

	/**
	 * Redirection processed on the activity documentation launching.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the resulting url to reach.
	 * @since July, 2011.
	 */
	public String onActivityDocumentation() {
		LOGGER.debug("onActivityDocumentation().");
		return "activityDocumentation.xhtml?faces-redirect=true";
	}

	/**
	 * Method called to redirect to analytical prioritizations page.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the url to follow.
	 * @since July, 2011.
	 */
	public String onAnalyticalPrioritizations() {
		LOGGER.debug("onAnalyticalPrioritizations().");
		prioritizationsToDo = null; // To trigger the data loading.
		return "analyticalPrioritizations.xhtml?faces-redirect=true";
	}

	/**
	 * Event listener to generate the variants of a procedural diagram.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param event the event launched on redrawing action.
	 * @since July, 2011.
	 */
	public void onGenerateVariants(ActionEvent event) {
		LOGGER.debug("onGenerateVariants");
		try {
			variantManager.createVariantsFromTestCaseId(currentTestCaseId,
					proceduralDiagramManager.generateVariantsFromProceduralDiagram(proceduralDiagramManager.createProceduralDiagramFromElementsAndTransitions(getElements(), getTransitions())));
		}
		catch (VariantCreationManagerException e) {
			LOGGER.error("Error during the variants generation.", e);
		}
	}

	/**
	 * Method called to redirect to the home page.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the url to follow.
	 * @since August, 2011.
	 */
	public String onHome() {
		LOGGER.debug("onHome().");
		activitiesToDescribe = null; // To trigger the data loading.
		prioritizationsToDo = null;
		testCasesVariantsToGenerate = null;
		return "index.xhtml?faces-redirect=true";
	}

	/**
	 * Getter for the private field value activityManager.
	 * 
	 * @return the activityManager field value.
	 */
	public ActivityManager getActivityManager() {
		return activityManager;
	}

	/**
	 * Setting a value to the activityManager field.
	 * 
	 * @param activityManager the value to set.
	 */
	public void setActivityManager(ActivityManager activityManager) {
		this.activityManager = activityManager;
	}

	/**
	 * Getter for the private field value activitiesToDescribe.
	 * 
	 * @return the activitiesToDescribe field value.
	 */
	public List<Activity> getActivitiesToDescribe() {
		if (null == activitiesToDescribe) {
			LOGGER.debug("getActivitiesToDescribe(): initialization.");
			try {
				activitiesToDescribe = activityManager.searchActivitiesToDescribe();
			}
			catch (ActivitySearchManagerException e) {
				LOGGER.error("Error during the recovery of the activities to describe.", e);
			}
		}
		return activitiesToDescribe;
	}

	/**
	 * Setting a value to the activitiesToDescribe field.
	 * 
	 * @param activitiesToDescribe the value to set.
	 */
	public void setActivitiesToDescribe(List<Activity> activitiesToDescribe) {
		this.activitiesToDescribe = activitiesToDescribe;
	}

	/**
	 * Getter for the private field value testCaseManager.
	 * 
	 * @return the testCaseManager field value.
	 */
	public TestCaseManager getTestCaseManager() {
		return testCaseManager;
	}

	/**
	 * Setting a value to the testCaseManager field.
	 * 
	 * @param testCaseManager the value to set.
	 */
	public void setTestCaseManager(TestCaseManager testCaseManager) {
		this.testCaseManager = testCaseManager;
	}

	/**
	 * Getter for the private field value testCasesVariantsToGenerate.
	 * 
	 * @return the testCasesVariantsToGenerate field value.
	 */
	public List<TestCase> getTestCasesVariantsToGenerate() {
		if (null == testCasesVariantsToGenerate) {
			LOGGER.debug("getTestCasesVariantsToGenerate(): initialization.");
			try {
				testCasesVariantsToGenerate = testCaseManager.searchTestCasesVariantsToGenerate();
			}
			catch (TestCaseSearchManagerException e) {
				LOGGER.error("Error during the recovery of the variants to generate.", e);
			}
		}
		return testCasesVariantsToGenerate;
	}

	/**
	 * Setting a value to the testCasesVariantsToGenerate field.
	 * 
	 * @param testCasesVariantsToGenerate the value to set.
	 */
	public void setTestCasesVariantsToGenerate(List<TestCase> testCasesVariantsToGenerate) {
		this.testCasesVariantsToGenerate = testCasesVariantsToGenerate;
	}

	/**
	 * Getter for the private field value elements.
	 * 
	 * @return the elements field value.
	 */
	public List<Element> getElements() {
		if (null == elements) {
			LOGGER.debug("getElements(): initialization.");
			try {
				elements = elementManager.searchElement(new ElementSearch(currentTestCaseId));
			}
			catch (ElementSearchManagerException e) {
				LOGGER.error("Error during the elements recovery.", e);
			}
		}
		return elements;
	}

	/**
	 * Setting a value to the elements field.
	 * 
	 * @param elements the value to set.
	 */
	public void setElements(List<Element> elements) {
		this.elements = elements;
	}

	/**
	 * Getter for the private field value transitions.
	 * 
	 * @return the transitions field value.
	 */
	public List<Transition> getTransitions() {
		if (null == transitions) {
			LOGGER.debug("getTransitions(): initialization.");
			try {
				transitions = transitionManager.searchTransition(new TransitionSearch(currentTestCaseId));
			}
			catch (TransitionSearchManagerException e) {
				LOGGER.error("Error during the transitions recovery.", e);
			}
		}
		return transitions;
	}

	/**
	 * Setting a value to the transitions field.
	 * 
	 * @param transitions the value to set.
	 */
	public void setTransitions(List<Transition> transitions) {
		this.transitions = transitions;
	}

	/**
	 * Getter for the private field value currentTestCaseId.
	 * 
	 * @return the currentTestCaseId field value.
	 */
	public int getCurrentTestCaseId() {
		return currentTestCaseId;
	}

	/**
	 * Setting a value to the currentTestCaseId field.
	 * 
	 * @param currentTestCaseId the value to set.
	 */
	public void setCurrentTestCaseId(int currentTestCaseId) {
		this.currentTestCaseId = currentTestCaseId;
	}

	/**
	 * Getter for the private field value transitionManager.
	 * 
	 * @return the transitionManager field value.
	 */
	public TransitionManager getTransitionManager() {
		return transitionManager;
	}

	/**
	 * Setting a value to the transitionManager field.
	 * 
	 * @param transitionManager the value to set.
	 */
	public void setTransitionManager(TransitionManager transitionManager) {
		this.transitionManager = transitionManager;
	}

	/**
	 * Getter for the private field value elementManager.
	 * 
	 * @return the elementManager field value.
	 */
	public ElementManager getElementManager() {
		return elementManager;
	}

	/**
	 * Setting a value to the elementManager field.
	 * 
	 * @param elementManager the value to set.
	 */
	public void setElementManager(ElementManager elementManager) {
		this.elementManager = elementManager;
	}

	/**
	 * Getter for the private field value proceduralDiagramManager.
	 * 
	 * @return the proceduralDiagramManager field value.
	 */
	public ProceduralDiagramManager getProceduralDiagramManager() {
		return proceduralDiagramManager;
	}

	/**
	 * Setting a value to the proceduralDiagramManager field.
	 * 
	 * @param proceduralDiagramManager the value to set.
	 */
	public void setProceduralDiagramManager(ProceduralDiagramManager proceduralDiagramManager) {
		this.proceduralDiagramManager = proceduralDiagramManager;
	}

	/**
	 * Getter for the private field value variantManager.
	 * 
	 * @return the variantManager field value.
	 */
	public VariantManager getVariantManager() {
		return variantManager;
	}

	/**
	 * Setting a value to the variantManager field.
	 * 
	 * @param variantManager the value to set.
	 */
	public void setVariantManager(VariantManager variantManager) {
		this.variantManager = variantManager;
	}

	/**
	 * Getter for the private field value prioritizationsToDo.
	 * 
	 * @return the prioritizationsToDo field value.
	 */
	public List<Iteration> getPrioritizationsToDo() {
		if (null == prioritizationsToDo) {
			LOGGER.debug("getPrioritizationsToDo(): initialization.");
			IterationSearch searchBean = new IterationSearch(((SessionController) FacesUtil.getControllerFromName(SESSION_CONTROLLER_NAME)).getSessionUser().getService().getServiceId());
			searchBean.setAnalyticalStatus(AssignmentStatus.ASSIGNED);
			try {
				List<Iteration> prioritizations = iterationManager.searchIteration(searchBean);
				for (Iteration prioritization : prioritizations) {
					prioritization.setVersion(versionManager.searchVersion(new VersionSearch(prioritization.getVersionId())).get(0));
					prioritization.setProject(projectManager.searchProject(new ProjectSearch(prioritization.getVersion().getProjectId())).get(0));
				}
				prioritizationsToDo = prioritizations;
			}
			catch (Exception e) {
				LOGGER.error("Error during the iteration search.", e);
			}
		}
		return prioritizationsToDo;
	}

	/**
	 * Setting a value to the prioritizationsToDo field.
	 * 
	 * @param prioritizationsToDo the value to set.
	 */
	public void setPrioritizationsToDo(List<Iteration> prioritizationsToDo) {
		this.prioritizationsToDo = prioritizationsToDo;
	}

	/**
	 * Getter for the private field value iterationManager.
	 * 
	 * @return the iterationManager field value.
	 */
	public IterationManager getIterationManager() {
		return iterationManager;
	}

	/**
	 * Setting a value to the iterationManager field.
	 * 
	 * @param iterationManager the value to set.
	 */
	public void setIterationManager(IterationManager iterationManager) {
		this.iterationManager = iterationManager;
	}

	/**
	 * Getter for the private field value projectManager.
	 * 
	 * @return the projectManager field value.
	 */
	public ProjectManager getProjectManager() {
		return projectManager;
	}

	/**
	 * Setting a value to the projectManager field.
	 * 
	 * @param projectManager the value to set.
	 */
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	/**
	 * Getter for the private field value versionManager.
	 * 
	 * @return the versionManager field value.
	 */
	public VersionManager getVersionManager() {
		return versionManager;
	}

	/**
	 * Setting a value to the versionManager field.
	 * 
	 * @param versionManager the value to set.
	 */
	public void setVersionManager(VersionManager versionManager) {
		this.versionManager = versionManager;
	}
}